﻿namespace TheCodeWerks.Advertising
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using System.Windows;
	using System.Windows.Controls;
	using InneractiveAdSDK;

	public class InnerActiveProvider : IAdvertisingProvider
	{
		private Dictionary<AdUnit, InneractiveSettings> adcontrols = new Dictionary<AdUnit, InneractiveSettings>(5);

		public AdvertisingConfiguration Config { get; set; }

		public AdProvider Provider { get { return AdProvider.InnerActive; } }

		public FrameworkElement CreateControl()
		{
			var grid = new Grid() { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
			grid.Children.Add(new Grid() { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center });

			return grid;
		}

		// this method can be cleaned up to act in a similar manner to the pubcenter provider
		public void ConfigureControl(FrameworkElement element, AdUnit unit)
		{
			var provider = Config.Providers[Provider];
			var grid = (Grid)((Grid)element).Children[0];

			if (!adcontrols.ContainsKey(unit))
			{
				var settings = new InneractiveSettings(Config.User)
				{
					AppId = Config.TestMode ? "IA_Test" : provider.Id
				};

				var keywords = new List<string>(Config.DefaultKeywords.Count + unit.Keywords.Count);
				keywords.AddRange(Config.DefaultKeywords);
				keywords.AddRange(unit.Keywords);

				settings.Parameters[InneractiveAd.IaOptionalParams.IaOptionalParams_Keywords] = string.Join(",", keywords.ToArray());

				if (!string.IsNullOrEmpty(unit.Type))
				{
					try
					{
						settings.Type = (InneractiveAd.IaAdType)Enum.Parse(typeof(InneractiveAd.IaAdType), unit.Type, true);
					}
					catch { }
				}

				grid.Tag = settings;
			}
			else grid.Tag = adcontrols[unit];
		}

		public void RequestNext(FrameworkElement element)
		{
			var grid = (Grid)((Grid)element).Children[0];
			var settings = (InneractiveSettings)grid.Tag;

			if (InneractiveAd.DisplayAd(settings.AppId, settings.Type, (Grid)element, 900, settings.Parameters))
				((AdEvents)element.Tag).AdReady();
			else
				((AdEvents)element.Tag).NotFilled();
		}

		private class InneractiveSettings
		{
			public string AppId;
			public Dictionary<InneractiveAd.IaOptionalParams, string> Parameters = new Dictionary<InneractiveAd.IaOptionalParams, string>();
			public InneractiveAd.IaAdType Type = InneractiveAd.IaAdType.IaAdType_Banner;

			public InneractiveSettings(UserInformation user)
			{
				if (user.Birthdate.HasValue)
				{
					Parameters.Add(InneractiveAd.IaOptionalParams.IaOptionalParams_Age, Convert.ToInt32((DateTime.Today - user.Birthdate.Value).Days / 365).ToString());
				}

				if (user.Gender != Gender.Unknown)
				{
					Parameters.Add(InneractiveAd.IaOptionalParams.IaOptionalParams_Gender, user.Gender.ToString());
				}

				Parameters.Add(InneractiveAd.IaOptionalParams.IaOptionalParams_Keywords, string.Empty);
			}
		}
	}
}
